package fr.micouz.gwt.dispatch.client;

import net.customware.gwt.dispatch.client.DispatchAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

import fr.micouz.gwt.dispatch.client.gin.MyGinjector;
import fr.micouz.gwt.dispatch.shared.MyAction;
import fr.micouz.gwt.dispatch.shared.MyResult;

public class Gwt_dispatch_example implements EntryPoint {

	private final MyGinjector injector = GWT.create(MyGinjector.class);
	private final DispatchAsync dispatcher = injector.getDispatchAsync();

	private TextBox textBox;
	private Label label;

	public void onModuleLoad() {

		Panel panel = new FlowPanel();
		RootPanel.get().add(panel);

		textBox = new TextBox();
		textBox.setText("my message");
		panel.add(textBox);

		Button button = new Button("Send Message");
		button.addClickHandler(new MyClickHandler());
		panel.add(button);

		label = new Label();
		panel.add(label);

	}

	private class MyClickHandler implements ClickHandler {

		public void onClick(ClickEvent event) {
			dispatcher.execute(new MyAction(textBox.getText()), new GotMyResult() {
				public void got(MyResult result) {
					label.setText(result.getMessage());
				}
			});
		}

	}
}
